$(function () {
    setButtonDisabled(true);
    var driver = $.cookie('sillyhat_sql_created_cookie_driver');
    var url = $.cookie('sillyhat_sql_created_cookie_url');
    var username = $.cookie('sillyhat_sql_created_cookie_username');
    var password = $.cookie('sillyhat_sql_created_cookie_password');
    $("#driver").val(driver);
    $("#url").val(url);
    $("#username").val(username);
    $("#password").val(password);
    refreshCookie(driver,url,username,password);
});

function testConnectionDatabase(){
    $.ajax({
        url : "${ctx}/sillyhat/admin/sqlcreated/testConnectionDatabase",
        async : false,
        error : function(data) {
            alert("网络错误");
        },
        data : {
            "driver" : $("#driver").val(),
            "url" : $("#url").val(),
            "username" : $("#username").val(),
            "password" : $("#password").val()
        },
        type : "get",
        dataType: "json",
        success : function(data){
            if(data != null){
                if(data.judge){
                    alert("连接成功");
                }else{
                    alert("连接失败");
                }
            }
        }
    });
}
function clearTable(){
    $("#showTableContentId tbody").html("<tr id=\"trSearchTableId\"></tr>");
}
function searchTable(){
    if($("#tableName").val() == ""){
        alert("需要输入表名");
        return;
    }
    $("#fieldTableName").val($("#tableName").val())
    setButtonDisabled(true);
    clearTable();
    $.ajax({
        url : "${ctx}/sillyhat/admin/sqlcreated/searchTable",
        async : false,
        error : function(data) {
            alert("网络错误");
        },
        data : {
            "driver" : $("#driver").val(),
            "url" : $("#url").val(),
            "username" : $("#username").val(),
            "password" : $("#password").val(),
            "tableName" : $("#tableName").val()
        },
        type : "get",
        dataType: "json",
        success : function(data){
            if(data != null){
                if(data.length > 0){
                    $("#fieldNumber").val(data.length)
                    setButtonDisabled(false);
                    $.each(data, function(idx, obj){
                        addRow(idx,obj);
                    });
//                            for (var i = 0; i < data.length; i++){
//                                addRow(data[i]);
//                            }
                }else{
                    alert("查询失败失败，数据库连接异常或查询表结构失败");
                }
            }
        }
    });
}

function addRow(rowIndex,obj){
    var isKey = "";
    if("1" == obj.IS_KEY){
        isKey = "主键";
    }
    var tr =
        "<tr id=\"createdSql_"+(rowIndex + 1)+"\" align='center'>" +
        "	<td>" + (rowIndex + 1) + "</td>" +
        "   <td>" + obj.COLUMN_NAME + "</td>" + "<input type=\"hidden\" id=\"fieldName_" + rowIndex + "\" value=\"" + obj.COLUMN_NAME + "\">" +
        "   <td>" + obj.DATA_TYPE + "</td>" + "<input type=\"hidden\" id=\"fieldType_" + rowIndex + "\" value=\"" + obj.DATA_TYPE + "\">" +
        "   <td>" + obj.COMMENTS + "</td>" + "<input type=\"hidden\" id=\"entityField_" + rowIndex + "\" value=\"" + obj.ENTITY_FIELD + "\">" +
        "   <td>" + isKey + "</td>" +
        "</tr>";
    $("#trSearchTableId").before(tr);
}
function saveCookie(){
    refreshCookie($("#driver").val(),$("#url").val(),$("#username").val(),$("#password").val());
    alert("保存成功");
}


function createdOracleSql(){
    var resultSql = getSelectAllSql("oracle") + "/n/r" + getSelectOneSql("oracle");
    $("#resultSql").val(resultSql);
}

function createdMybatisSql(){
    var resultSql = getSelectAllSql("mybatis") + "/n/r" + getSelectOneSql("mybatis");
    $("#resultSql").val(resultSql);
}

function setButtonDisabled(btnDisabled) {
    $("#btnCreatedOracleSqlId").attr('disabled',btnDisabled);//设置disabled属性为false，按钮可用
    $("#btnCreatedMybatisSqlId").attr('disabled',btnDisabled);//设置disabled属性为true，按钮不可用
}

function refreshCookie(driver,url,username,password){
    $.cookie('sillyhat_sql_created_cookie_driver', driver, { expires: 30 });
    $.cookie('sillyhat_sql_created_cookie_url', url, { expires: 30 });
    $.cookie('sillyhat_sql_created_cookie_username',username, { expires: 30 });
    $.cookie('sillyhat_sql_created_cookie_password',password, { expires: 30 });
}

/**
 * 查询全部
 */
function getSelectAllSql(type){
    if("oracle" == type){
        return "getSelectAllSql-----oracle";
    }else if("mybatis" == type){
        return "getSelectAllSql-----mybatis";
    }
}

/**
 * 查询一个
 */
function getSelectOneSql(type){
    if("oracle" == type){
        return "getSelectOneSql-----oracle";
    }else if("mybatis" == type){
        return "getSelectOneSql-----mybatis";
    }
}

/**
 * 分页查询
 */
function getSelectPageSql(){

}
/**
 * 分页查询记录数
 */
function getSelectPageCountSql(){

}
/**
 * 修改
 */
function getUpdateSql(){

}
/**
 * 新增
 */
function getInsertSql(){

}
/**
 * 删除
 */
function getDeleteSql(){

}